package com.symaster.backups.frame.dialog;

import cn.hutool.core.io.IoUtil;
import com.symaster.backups.frame.MainFrame;
import com.symaster.backups.manager.TaskManager;
import com.symaster.common.util.IOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author yinmiao
 * @version 2024/2/23
 */
@Component
public class BackupFileMove extends JDialog {

    private static final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1024));
    private final JLabel jLabel = new JLabel();
    private final TaskManager taskManager;

    @Autowired
    public BackupFileMove(TaskManager taskManager) {
        this.taskManager = taskManager;
        setContentPane(jLabel);
        setPreferredSize(new Dimension(400, 100));
        pack();
    }

    public void open(String srcFolder, String dstFolder) {

        File src = new File(srcFolder);
        File dst = new File(dstFolder);

        threadPool.execute(() -> {

            jLabel.setText("正在停止文件监控");
            taskManager.stopTask("FileMonitor");

            moveDirectory(src, dst);

            jLabel.setText("启动文件监控");
            taskManager.startTask("FileMonitor");
            jLabel.setText("迁移完成");
        });

        setVisible(true);
    }

    private void moveDirectory(File file, File dstFile) {
        if (!dstFile.isDirectory() && !dstFile.mkdirs()) {
            folderCreateError();
            return;
        }

        File[] files = file.listFiles();
        if (files != null) {
            for (File file1 : files) {
                File file2 = new File(dstFile.toPath() + File.separator + file1.getName());
                if (file1.isDirectory()) {
                    moveDirectory(file1, file2);
                } else if (file1.isFile()) {
                    moveFile(file1, file2);
                }
            }
        }

        File[] files1 = file.listFiles();
        if (files1 == null || files1.length == 0) {
            boolean delete = file.delete();

        }
    }

    private void moveFile(File file, File dstFile) {
        jLabel.setText(String.format("正在迁移文件(%s)", file.toPath()));

        boolean b = file.renameTo(dstFile);
        if (!b) {

            FileOutputStream fileOutputStream = null;
            FileInputStream fileInputStream = null;
            try {
                fileOutputStream = new FileOutputStream(dstFile);
                fileInputStream = new FileInputStream(file);
                long copy = IoUtil.copy(fileInputStream, fileOutputStream);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } finally {
                IOUtil.close(fileInputStream, fileOutputStream);
            }
        }
    }

    private static void folderCreateError() {
        JOptionPane.showMessageDialog(null, "目标文件夹创建失败", "错误", JOptionPane.ERROR_MESSAGE);
    }


}
